package com.jt.demos;

import redis.clients.jedis.Jedis;

import java.util.Set;

/**
 * 设计一个基于某个活动投票系统
 */
public class VoteDemo01 {
   private static Jedis jedis = new Jedis("192.168.126.128",6379);
   static boolean checkVote(String activityId,String userId){
       return jedis.sismember(activityId, userId);
    }
    static boolean addVote(String activityId,String userId){
       if (checkVote(activityId, userId)){
           System.out.println("你已经参与这个投票");
           return false;
       }
       jedis.sadd(activityId, userId);
       return true;
    }
    static Long getActivityVotes(String activityId){
       return jedis.scard(activityId);
    }
    static Set<String> getActivityUsers(String activityId){
       return jedis.smembers(activityId);
    }
    public static void main(String[] args) {
        //1.设置一个活动
        //1.)获取ID
        String activityId = "10001";
        //2.)活动标题
        //3.)活动内容

        //2.基于活动进行投票设计
        String user1 = "201";
        String user2 = "202";
        String user3 = "203";
        //2.1 一个用户对同一个活动不能重复投票
       // boolean b = checkVote(activityId, user1);

        //2.2 执行投票
        addVote(activityId,user1);
        addVote(activityId,user2);
        addVote(activityId,user3);
        //2.3 能够显示参与投票的人数
        Long votes = getActivityVotes(activityId);
        System.out.println(votes);
        //2.4 管路员可以查看那些人参与了投票
        Set<String> set = getActivityUsers(activityId);
        System.out.println(set);

    }
}
